package com.cskaoyan.config;

import com.cskaoyan.advice.CountTimeAdvice;
import com.cskaoyan.service.UserService;
import com.cskaoyan.service.UserServiceImpl;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * @author stone
 * @date 2022/05/27 10:56
 */
@Configuration
@ComponentScan("com.cskaoyan")
@EnableAspectJAutoProxy
public class SpringConfiguration {

    // 注册UserServiceImpl对应的组件
    //@Bean // 组件id是方法名userService
    @Bean("userServiceImpl") // 组件是value属性值userServiceImpl
    public UserService userService() {
        UserServiceImpl userService = new UserServiceImpl();
        return userService;
    }

    /*@Bean
    public CountTimeAdvice countTimeAdvice() {
        return new CountTimeAdvice();
    }*/

    //@Autowired
    //UserService userService;   // 从容器中取出组件

    @Bean
    public ProxyFactoryBean userServiceProxy(@Qualifier("userServiceImpl") UserService userService) {
        ProxyFactoryBean proxyFactoryBean = new ProxyFactoryBean();
        proxyFactoryBean.setTarget(userService);
        proxyFactoryBean.setInterceptorNames("countTimeAdvice");
        return proxyFactoryBean;
    }
}
